package com.dragonfly.shutter.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.beans.PagedBean;
import com.dragonfly.shutter.services.CollectionListService;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 收藏列表控制类
 * @author 黄挺
 *
 */
@Controller
@RequestMapping("/collectionList.html")
public class CollectionListController {
	@Autowired
	private CollectionListService collectionListService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView onLoad(HttpSession session, HttpServletRequest request,
			@RequestParam(value="page", required=false) String pageStr){
		Integer userID = (Integer) session.getAttribute("userID");
		Message message = null;
		
		//尚未登录的用户
		if(userID == null ){
			message = new Message("您尚未登录，请先登录", "未登录", "login.html");
			return new ModelAndView("message", "message", message);
		}
		
		//空的参数
		if(pageStr == null){
			message = new Message("空的参数", "操作失败", "login.html");
			return new ModelAndView("message", "message", message);
		}
		
		//非法的参数
		if(!KeywordFilter.isNumeric(pageStr)){
			message = new Message("非法的参数", "操作失败", "login.html");
			return new ModelAndView("message", "message", message);
		}
		
		Integer page = Integer.parseInt(pageStr);
		
		PagedBean collections = collectionListService.doService(userID, page, request.getRequestURL().toString());
		
		return new ModelAndView("collectionList", "collections", collections);
	}
}
